package com.itheima.controller;

import com.itheima.dto.ClassQueryRequest;
import com.itheima.dto.PageBean;
import com.itheima.pojo.Clazz;
import com.itheima.service.ClassService;
import com.itheima.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/clazzs")
@Slf4j
public class ClassController {

    @Autowired
    private ClassService classService;



    @GetMapping
    public Result<PageBean> page(ClassQueryRequest classQueryRequest) {
        PageBean<Clazz> pageBean= classService.page(classQueryRequest);
        return Result.success(pageBean);
    }

    @PostMapping
    public Result insert(@RequestBody Clazz clazz) {
        classService.insert(clazz);
        return Result.success("新增成功");
    }

    @GetMapping("/{id}")
    public Result<Clazz> getById(@PathVariable Integer id) {
        Clazz clazz=classService.getById(id);
        return Result.success(clazz);
    }

    @PutMapping
    public Result<String> update(@RequestBody Clazz clazz) {
        classService.update(clazz);
        return Result.success("修改成功");
    }

    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Integer id) {
        classService.delete(id);
        return Result.success("删除成功");
    }

    @GetMapping("/list")
    public Result<List<Clazz>> getClazzs() {
       List<Clazz> clazzes= classService.getClazzs();
       return Result.success(clazzes);
    }

}
